﻿using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace BlawkayEngine
{
    public class Circle
    {

        #region Constructors

        /// <summary>
        /// Creates a Circle. Starts at (0,0).
        /// </summary>
        /// <param name="oRadius">Initial Radius</param>
        public Circle(float oRadius)
            : this(Vector2.Zero, oRadius)
        { }

        /// <summary>
        /// Creates a Circle.
        /// </summary>
        /// <param name="oPosition">Starting Position</param>
        /// <param name="oRadius">Initial Radius</param>
        public Circle(Vector2 oPosition, float oRadius)
        {
            Position = oPosition;
            Radius = oRadius;
        }

        #endregion

        #region Methods

        public static Rectangle NonRotateSquare(Circle a)
        {
            Vector2 oPos = a.Position;
            int oRadius = (int)a.Radius;

            int x = (int)oPos.X - oRadius;
            int y = (int)oPos.Y - oRadius;
            int w = (int)oPos.X + oRadius - x;
            int h = (int)oPos.Y + oRadius - y;

            return new Rectangle(x, y, w, h);
        }

        /// <summary>
        /// Determines if two Circles are Colliding.
        /// </summary>
        /// <param name="a">Circle A</param>
        /// <param name="b">Circle B</param>
        /// <returns>True if A and B are Colliding</returns>
        public static bool Collision(Circle a, Circle b)
        {
            float oMaxDistance
                = a.Radius + b.Radius;

            float oDistance
                = Vector2.Distance(a.Position, b.Position);

            if (oDistance <= oMaxDistance)
                return true;

            return false;
        }

        /// <summary>
        /// Determines if a Circle and a Rectangle are Colliding.
        /// </summary>
        /// <param name="a">Circle A</param>
        /// <param name="b">Rectangle B</param>
        /// <returns>True if A and B are Colliding</returns>
        public static bool Collision(Circle a, Rectangle b)
        {
            Rectangle oRectA = NonRotateSquare(a);

            if(oRectA.Intersects(b))
                return true;

            return false;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Returns the Diameter
        /// </summary>
        public float Diameter
        {
            get { return Radius * 2; }
        }

        /// <summary>
        /// Returns the Radius
        /// </summary>
        public float Radius
        {
            get { return _Radius; }
            set
            {
                if (value > 0 && value < 10000)
                    _Radius = value;
            }
        }

        /// <summary>
        /// Returns the Position
        /// </summary>
        public Vector2 Position
        {
            get { return _Position; }
            set { _Position = value; }
        }

        #endregion

        #region Fields

        private Vector2 _Position;
        private float _Radius;

        #endregion

    }
}
